package nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * description:
 * author:zt
 * date:2021-09-10
 */
public class NIOFileChannel02 {
    public static void main(String[] args) throws Exception {
        //创建文件输入流
        File file = new File("d:\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel channel = fileInputStream.getChannel();

        //创建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
        //将通道的数据读入到buffer
        channel.read(buffer);

        //将buffer的字节数据转成String
        System.out.println(new String(buffer.array()));
        fileInputStream.close();
    }
}
